package com.wardrobe.soa.api.home;

import com.wardrobe.dao.banner.BannerDao;
import com.wardrobe.dao.booking.BookingDao;
import com.wardrobe.dao.goods.GoodsDao;
import com.wardrobe.dao.train_manage.TrainManageDao;
import com.wardrobe.domain.banner.BannerShowBean;
import com.wardrobe.domain.booking.ApiBookingShowBean;
import com.wardrobe.domain.booking.BookingShowBean;
import com.wardrobe.domain.goods.GoodsShowBean;
import com.wardrobe.domain.train_manage.TrainManageShowBean;
import com.wardrobe.soa.utils.LocationUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * Created by Administrator on 2018/11/15.
 */
@Service
public class HomeInfoService {
    @Autowired
    private BannerDao bannerDao;
    @Autowired
    private TrainManageDao trainManageDao;
    @Autowired
    private GoodsDao goodsDao;
    @Autowired
    private BookingDao bookingDao;

    public HomeInfoResponse getHomeInfo(double longitude,double latitude){
        //轮播图
        List<BannerShowBean> bannerShowBeans = bannerDao.selectBannerList(1, 1);
        bannerShowBeans.stream().forEach(bannerShowBean -> {
            bannerShowBean.setCreateTime(bannerShowBean.getCreateTime().substring(0,bannerShowBean.getCreateTime().length()-2));
            bannerShowBean.setUpdateTime(bannerShowBean.getUpdateTime().substring(0,bannerShowBean.getUpdateTime().length()-2));
        });
        //banner图
        List<BannerShowBean> bannerPictureList = bannerDao.selectBannerList(1,2);
        //培训管理
        List<TrainManageShowBean> trainManage = trainManageDao.selectTrainManageList();
        trainManage.stream().forEach(trainMan ->{
            trainMan.setCreateTime(trainMan.getCreateTime().substring(0,trainMan.getCreateTime().length()-2));
            trainMan.setUpdateTime(trainMan.getUpdateTime().substring(0,trainMan.getUpdateTime().length()-2));
            trainMan.setContent("");
        });
        String bannerPicture = null;
        if(bannerPictureList.size()>0){
            bannerPicture = bannerPictureList.get(0).getImage();
        }
        //预约整理
        List<BookingShowBean> bookingList = bookingDao.selectApiBookingList();
        bookingList.stream().forEach(booking ->booking.setDistance(LocationUtils.getDistance(Double.parseDouble(booking.getLatitude()),Double.parseDouble(booking.getLongitude()),latitude,longitude)));
        //按距离排序
        bookingList.stream().forEach(book ->book.setShowPicture(book.getShowPicture().split(";")[0]));
        LocationUtils.quickSort(bookingList,0,bookingList.size() - 1,1,"ASC");
        List<ApiBookingShowBean> bookingShowList = bookingList.stream().map(bookingBean -> new ApiBookingShowBean(bookingBean.getId(),bookingBean.getShopId(),bookingBean.getName(),bookingBean.getShowPicture(),LocationUtils.getDisStr("距离：",bookingBean.getDistance()),bookingBean.getWatch()))
                .collect(Collectors.toList());
        //发现好物
        List<GoodsShowBean> goodsList = goodsDao.selectApiGoodsList();
        goodsList.stream().forEach(x->{
            x.setCreateTime(x.getCreateTime().substring(0,x.getCreateTime().length()-2));
            x.setUpdateTime(x.getUpdateTime().substring(0,x.getUpdateTime().length()-2));
        });
        return HomeInfoResponse.builder()
                .bannerList(bannerShowBeans)
                .bannerPicture(bannerPicture)
                .trainManageList(trainManage.size()>6?trainManage.subList(0,6):trainManage)
                .bookingList(bookingShowList.size()>4?bookingShowList.subList(0,4):bookingShowList)
                .goodsList(goodsList)
                .build();
    }
}
